package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.CopyrightAuctionRound;
import com.sc.nft.entity.CopyrightAuctionUserBidding;
import com.sc.nft.entity.vo.CopyrightAuctionRoundUserListVO;
import com.sc.nft.entity.vo.CopyrightAuctionUserBiddingAdminDataVO;
import com.sc.nft.entity.vo.CopyrightAuctionUserBiddingAdminListVO;
import com.sc.nft.enums.CopyrightCollectionStatusEnum;
import com.sc.nft.mapper.CopyrightAuctionUserBiddingMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @Author：LiGen
 * @Package：com.sc.nft.dao
 * @Project：sc_metago
 * @name：CopyrightAuctionDao
 * @Date：2024/9/12 17:39
 * @Filename：CopyrightAuctionDao
 */
@Slf4j
@Repository
public class CopyrightAuctionUserBiddingDao extends ServiceImpl<CopyrightAuctionUserBiddingMapper, CopyrightAuctionUserBidding> {
    public Integer countByRoundIdAndUserId(Long recordId, Long roundId, Long userId) {
        LambdaQueryWrapper<CopyrightAuctionUserBidding> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRecordId, recordId);
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRoundId, roundId);
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getUserId, userId);
        return count(lambdaQueryWrapper);
    }


    public CopyrightAuctionUserBidding sumByRoundIdAndUserId(Long recordId,Long roundId, Long userId) {
        LambdaQueryWrapper<CopyrightAuctionUserBidding> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRecordId,recordId);
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRoundId,roundId);
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getUserId,userId);
        lambdaQueryWrapper.last("order by id desc limit 1");
        return getOne(lambdaQueryWrapper);
    }

    /**
     * 拍卖汇总记录
     *
     * @param copyrightAuctionRecordId
     * @param mp
     * @return
     */
    public CopyrightAuctionUserBiddingAdminDataVO copyrightAuctionRecordAdminData(Long copyrightAuctionRecordId) {
        return baseMapper.copyrightAuctionRecordAdminData(copyrightAuctionRecordId);
    }

    /**
     * 出价用户列表
     *
     * @param copyrightAuctionRecordId
     * @param mp
     * @return
     */
    public IPage<CopyrightAuctionUserBiddingAdminListVO> copyrightAuctionUserBiddingAdminList(Long copyrightAuctionRecordId, String userTel, Page mp,Long roundId) {
        return baseMapper.copyrightAuctionUserBiddingAdminList(mp, copyrightAuctionRecordId, userTel,roundId);
    }

    public CopyrightAuctionUserBidding getByRecordIdAndRoundIdAndUserId(Long recordId, Long roundId, Long userId) {
        LambdaQueryWrapper<CopyrightAuctionUserBidding> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRecordId, recordId);
        queryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRoundId, roundId);
        queryWrapper.eq(CopyrightAuctionUserBidding::getUserId, userId);
        queryWrapper.last("limit 1");
        return getOne(queryWrapper);
    }

    public Boolean updateBiddingNum(int biddingNum, Long id, Date bidTime) {
        LambdaUpdateWrapper<CopyrightAuctionUserBidding> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CopyrightAuctionUserBidding::getId, id);
        updateWrapper.set(CopyrightAuctionUserBidding::getBiddingNum, biddingNum);
        updateWrapper.set(CopyrightAuctionUserBidding::getBidTime, bidTime);
        return update(updateWrapper);
    }

    public List<CopyrightAuctionUserBidding> getList(Long copyrightAuctionRecordId, Long copyrightAuctionRoundId) {
        LambdaQueryWrapper<CopyrightAuctionUserBidding> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRoundId, copyrightAuctionRecordId);
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRoundId, copyrightAuctionRoundId);
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getStatus, CopyrightCollectionStatusEnum.UNDER_PLEDGE);
        lambdaQueryWrapper.last("order by bidding_num desc,bid_time ASC");
        return list(lambdaQueryWrapper);
    }


    /**
     * 更新本轮最高价
     *
     * @param id
     * @param roundHighest
     * @return
     */
    public boolean updateRoundHighest(Long id, Boolean roundHighest) {
        LambdaUpdateWrapper<CopyrightAuctionUserBidding> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(CopyrightAuctionUserBidding::getId, id);
        queryWrapper.set(CopyrightAuctionUserBidding::getRoundHighest, roundHighest);
        return this.update(queryWrapper);
    }

    public boolean updateStatusById(Long id, CopyrightCollectionStatusEnum copyrightCollectionStatusEnum) {
        LambdaUpdateWrapper<CopyrightAuctionUserBidding> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CopyrightAuctionUserBidding::getId, id);
        updateWrapper.set(CopyrightAuctionUserBidding::getStatus, copyrightCollectionStatusEnum);
        return update(updateWrapper);
    }

    /**
     * 更改出价表状态
     *
     * @param ids
     * @param status
     * @return
     */
    public boolean updateStatus(List<Long> ids, CopyrightCollectionStatusEnum status,Boolean progress) {
        LambdaUpdateWrapper<CopyrightAuctionUserBidding> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.in(CopyrightAuctionUserBidding::getId, ids);
        queryWrapper.eq(CopyrightAuctionUserBidding::getStatus, CopyrightCollectionStatusEnum.UNDER_PLEDGE);
        queryWrapper.set(CopyrightAuctionUserBidding::getStatus, status);
        queryWrapper.set(progress!=null, CopyrightAuctionUserBidding::getProgress, progress);
        return this.update(queryWrapper);
    }


    /**
     * 中标处理
     * @param id
     * @param roundHighest
     * @param progress
     * @param status
     * @return
     */
    public boolean biddingProcess(Long id, Boolean roundHighest, Boolean progress, CopyrightCollectionStatusEnum status) {
        LambdaUpdateWrapper<CopyrightAuctionUserBidding> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(CopyrightAuctionUserBidding::getId, id);
        queryWrapper.set(CopyrightAuctionUserBidding::getStatus, status);
        queryWrapper.set(CopyrightAuctionUserBidding::getRoundHighest, roundHighest);
        queryWrapper.set(CopyrightAuctionUserBidding::getProgress, progress);
        return this.update(queryWrapper);
    }

    /**
     * 获取拍卖每一轮的最高出价表
     *
     * @param copyrightAuctionRecordId
     * @return
     */
    public List<CopyrightAuctionUserBidding> getIsHighestList(Long copyrightAuctionRecordId) {
        LambdaQueryWrapper<CopyrightAuctionUserBidding> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRecordId, copyrightAuctionRecordId);
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getRoundHighest, Boolean.TRUE);
        return list(lambdaQueryWrapper);
    }

    public List<CopyrightAuctionUserBidding> getByRecordId(Long copyrightAuctionRecordId) {
        LambdaQueryWrapper<CopyrightAuctionUserBidding> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRecordId, copyrightAuctionRecordId);
        return list(lambdaQueryWrapper);
    }

    public CopyrightAuctionUserBidding getHighByRecordIdAndRoundId(Long recordId, Long roundId) {
        LambdaQueryWrapper<CopyrightAuctionUserBidding> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRecordId, recordId);
        lambdaQueryWrapper.eq(CopyrightAuctionUserBidding::getCopyrightAuctionRoundId, roundId);
        lambdaQueryWrapper.last("order by bidding_num desc limit 1");
        return getOne(lambdaQueryWrapper);
    }
}
